package com_djm_2025_06_10.demo1;

import java.util.Arrays;
import java.util.Objects;

class Student implements Comparable<Student>{
    public String name;

    public int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && name.equals(student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public int compareTo(Student student) {
        //如果要逆序怎么办呢？
        return this.age - student.age;
    }
}
public class Test {
    //利用冒泡排序模拟实现Arrays.sort()
    public static void mySort(Comparable[] comparables) {
        //外部循环代表数组长度
        for (int i = 0; i < comparables.length; i++) {
            //内部循环代表排序趟数
            for (int j = 0; j < comparables.length - 1 - i; j++) {
                if(comparables[j].compareTo(comparables[j+1]) > 0) {
                    Comparable tmp = comparables[j];
                    comparables[j] = comparables[j+1];
                    comparables[j+1] = tmp;
                }
            }

        }
    }

    public static void main1(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("djm",22);
        students[1] = new Student("zzx",23);
        students[2] = new Student("tl",22);

        System.out.println("排序前：" + Arrays.deepToString(students));

        //一条华丽的分割线
        System.out.println("----------------------");

//        Arrays.sort(students);
        mySort(students);
        System.out.println("排序后：" + Arrays.deepToString(students));
    }

    public static void main2(String[] args) {

        Student[] students = new Student[3];
        students[0] = new Student("djm",22);
        students[1] = new Student("zzx",23);
        students[2] = new Student("tl",22);

        AgeComparetor ageComparetor = new AgeComparetor();
        System.out.println(ageComparetor.compare(students[0],students[1]));

    }

    public static void main3(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("djm",22);
        students[1] = new Student("zzx",23);
        students[2] = new Student("tl",22);

        System.out.println("排序前："+Arrays.toString(students));
//        AgeComparetor ageComparetor = new AgeComparetor();
        NameComparetor nameComparetor = new NameComparetor();
        //给Arrays一个排序的规则
        Arrays.sort(students,nameComparetor);

        //一条华丽的分割线
        System.out.println("---------------------------------");

        System.out.println("排序后："+Arrays.toString(students));

    }

    public static void main(String[] args) {
        Student student1 = new Student("djm",22);
        Student student2 = new Student("djm",22);

        System.out.println(student1 == student2);

        System.out.println(student1.equals(student2));
    }
}
